//
//  TSHomeViewController.m
//  chart
//
//  Created by xu on 15/8/3.
//  Copyright (c) 2015年 xu. All rights reserved.
//

#import "TSHomeViewController.h"
#import "AppDelegate.h"
#import "TSUserInfoViewController.h"
#import "TSLoginViewController.h"
#import "TSJournalTabBarController.h"
#import "YZSwipeBetweenViewController.h"
#import "TSPasswordEditViewController.h"
#import "TSReceiveTabBarController.h"
#import "TSMsgTabBarController.h"
#import "DoImagePickerController.h"
#define MARGIN_GAP_HOR 30.f
#define TITLE_HEIGHT 20.f
#define SMALL_BUTTON_WIDTH 60.f
#define SMALL_TITLE_HEIGHT 16.f
#define TOP_MARGIN 140.f
#define HORIZONTAL_SPACING 20.f//水平间距
#define VERTICAL_SPACING 6.f//垂直间距
#define VERTICAL_SPACING_SMALL 3.f//垂直小间距
#define BUTTON_HEIGHT 50.f
@interface TSHomeViewController ()

@end

@implementation TSHomeViewController
{
    NSArray *titleArray;
    NSArray *imageNameArray;
    float btnWidth;
    float btnHeight;
    UIScrollView * sview;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    btnWidth = (currentDeviceWidth-HORIZONTAL_SPACING*3)/2;
    btnHeight = (btnWidth*378/444-VERTICAL_SPACING_SMALL)/2;
    self.title = @"青山政务";
    self.navigationController.navigationBarHidden = NO;
    UIImage*draw = [UIImage imageNamed:@"main_bg"];
    UIImageView *drawView = [[UIImageView alloc]initWithImage:draw];
    [drawView setFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:drawView];
    sview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, TOP_MARGIN, currentDeviceWidth, currentDeviceHeight-240)];
    [sview setContentSize:CGSizeMake(currentDeviceWidth, (btnHeight*2+VERTICAL_SPACING_SMALL)*3+VERTICAL_SPACING*2)];
    sview.scrollEnabled = YES;
    [self.view addSubview:sview];
    
    WS(weakSelf);
    [self leftBarButtonWithTitle:nil barNorImage:[UIImage imageNamed:@"nav_back"] barHltImage:nil action:^{
        if (!weakSelf) {
            return;
        }
        [weakSelf onLeftBarButtonClick];
    }];
    
    [self rightBarButtonWithTitle:@"个人" barNorImage:nil barHltImage:nil action:^{
        if (!weakSelf) {
            return;
        }
        
        [weakSelf showUserInfoViewController];
    }];
    // @"icon_search_task", @"icon_submit_task", @"icon_statistics", @"icon_search", @"icon_other_function", @"icon_project_track", @"icon_task_progress"
    // @"任务查询", @"任务提交", @"统计分析",  @"项目跟踪", @"任务进展"
    
    imageNameArray = @[ @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9"];
    
    [self addAllButtons:imageNameArray];
    
    [self addBottomButtons];
}

-(void)addBottomButtons
{
    imageNameArray = @[ @"便捷助手1", @"统计分析2", @"个人设置3"];
    titleArray = @[@"便捷助手", @"统计分析", @"个人设置"];
    UIButton *btn;
    UILabel  *label;
    NSInteger gap = (currentDeviceWidth - 3*SMALL_BUTTON_WIDTH)/4;
    for (int i=0; i<imageNameArray.count; i++) {
        btn = [self createHomeButton:CGRectMake(gap+(SMALL_BUTTON_WIDTH+gap)*(i%3), currentDeviceHeight-100 , SMALL_BUTTON_WIDTH, SMALL_BUTTON_WIDTH) WithImageName:[imageNameArray objectAtIndex:i]];
        [btn setTag:i+9];
        [self.view addSubview:btn];
        label = [self createLabel:CGRectMake(CGRectGetMinX(btn.frame), CGRectGetMaxY(btn.frame), SMALL_BUTTON_WIDTH, SMALL_TITLE_HEIGHT) WithTitle:[titleArray objectAtIndex:i]];
        [label setTag:i+9];
        [self.view addSubview:label];
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden=YES;
}


- (void)onLeftBarButtonClick
{
    TSLoginViewController *login_vc = [[TSLoginViewController alloc] init];
    TSBaseNavigationController *nav_vc = [[TSBaseNavigationController alloc] initWithRootViewController:login_vc];
    [self presentViewController:nav_vc animated:YES completion:nil];
}

- (void)addAllButtons:(NSArray *)imageNames
{
    //   UIButton *btn;
    //    UILabel  *label;
    //    NSInteger gap = (currentDeviceWidth - 3*BUTTON_WIDTH)/4;
    //    NSInteger p6_margin = 0;
    //    if(currentDeviceHeight==736){
    //        p6_margin = 30;
    //    }
    //    for (int i=0; i<titles.count; i++) {
    //        btn = [self createHomeButton:CGRectMake(gap+(BUTTON_WIDTH+gap)*(i%3), TOP_MARGIN + (i/3) * (MARGIN_GAP_HOR + BUTTON_WIDTH)+p6_margin, BUTTON_WIDTH, BUTTON_WIDTH) WithImageName:[imageNames objectAtIndex:i]];
    //        [btn setTag:i];
    //        [self.view addSubview:btn];
    //
    //        label = [self createLabel:CGRectMake(CGRectGetMinX(btn.frame), CGRectGetMaxY(btn.frame), BUTTON_WIDTH, TITLE_HEIGHT) WithTitle:[titles objectAtIndex:i]];
    //        [label setTag:i];
    //        [self.view addSubview:label];
    //    }
    UIButton *btn;
    for(int i=0; i<imageNames.count;i++){
        
        if(i>=6){
            int left = (i-5)%2*(btnWidth+HORIZONTAL_SPACING)+HORIZONTAL_SPACING;
            int top = (i-6)*(btnHeight*2+VERTICAL_SPACING_SMALL+VERTICAL_SPACING);
            btn = [self createHomeButton:CGRectMake(left,top, btnWidth, btnHeight*2+VERTICAL_SPACING_SMALL) WithImageName:[imageNames objectAtIndex:i]];
            [btn setTag:i];
            [sview addSubview:btn];
        }else{
            int left =  i/2%2*(btnWidth+HORIZONTAL_SPACING)+HORIZONTAL_SPACING;
            int top = i*btnHeight+(i/2*(VERTICAL_SPACING_SMALL+VERTICAL_SPACING)+i%2*VERTICAL_SPACING_SMALL);
            btn = [self createHomeButton:CGRectMake(left,top,btnWidth,btnHeight) WithImageName:[imageNames objectAtIndex:i]];
            [btn setTag:i];
            [sview addSubview:btn];
        }
    }
    
}

- (UIButton *)createHomeButton:(CGRect)frame WithImageName:(NSString *)imageName
{
    UIButton *btn = [[UIButton alloc] initWithFrame:frame];
    [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(onButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    return  btn;
}

- (void)onButtonClick:(UIButton *)sender
{
    /* 事件通知 */
    if (sender.tag == 6) {
        TSMsgTabBarController *tab_bar = [[TSMsgTabBarController alloc] init];
        [self presentViewController:tab_bar animated:YES completion:nil];
    }
    //个人日程
    else if (sender.tag == 0) {
        TSJournalTabBarController *tab_bar = [[TSJournalTabBarController alloc] init];
        [self presentViewController:tab_bar animated:YES completion:nil];
    }
    //智能查询
    else if (sender.tag == 1) {
        TSMsgTabBarController *tab_bar = [[TSMsgTabBarController alloc] init];
        [self presentViewController:tab_bar animated:YES completion:nil];
    }
    //项目跟踪
    else if (sender.tag == 2) {
        DoImagePickerController *cont = [[DoImagePickerController alloc] initWithNibName:@"DoImagePickerController" bundle:nil];
        cont.delegate = self;
        cont.nMaxCount = 4;     // larger than 1
        cont.nColumnCount = 3;  // 2, 3, or 4
        
        cont.nResultType = DO_PICKER_RESULT_UIIMAGE; // get UIImage object array : common case
        // if you want to get lots photos, you had better use DO_PICKER_RESULT_ASSET.
        
        [self presentViewController:cont animated:YES completion:nil];
    }
    //办公协同
    else if (sender.tag == 3) {
    }
    //个人设置
    else if(sender.tag == 11){
        [self showUserInfoViewController];
    }/* 任务跟踪 */
    if (sender.tag == 7) {
         TSReceiveTabBarController *tab_bar = [[TSReceiveTabBarController alloc] init];
        [self presentViewController:tab_bar animated:YES completion:nil];
    }
}

- (void)showUserInfoViewController
{
    YZSwipeBetweenViewController *scroll_vc =[YZSwipeBetweenViewController new];
    UIViewController *user_vc= [[TSUserInfoViewController alloc] init];
    UINavigationController *navCon1 =
    [[UINavigationController alloc]initWithRootViewController:user_vc];
    UIViewController *pwd_vc = [[TSPasswordEditViewController alloc] init];
    UINavigationController *navCon2 =
    [[UINavigationController alloc] initWithRootViewController:pwd_vc];
    scroll_vc.viewControllers = @[navCon1,navCon2];
    scroll_vc.initialViewControllerIndex=0;
    [self.navigationController pushViewController:scroll_vc animated:YES];
}

- (void)setTabBarSelectedItem:(NSInteger)index
{
    AppDelegate *app_delegate = [UIApplication sharedApplication].delegate;
    app_delegate.tabBarController.selectedIndex = index;
}

- (UILabel *)createLabel:(CGRect)frame WithTitle:(NSString *)title
{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    [label setText:title];
    [label setFont:FONT(12.f)];
    [label setTextAlignment:NSTextAlignmentCenter];
    
    return label;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
